﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class FormServers
    {
	    private readonly IRepository<wf_forminfo> wf_forminfoRepository;
        private readonly IRepository<wf_formfieldinfo> wf_formfieldinfoRepository;
        public FormServers()
        {
            wf_forminfoRepository = new DapperRepository<wf_forminfo>();
            wf_formfieldinfoRepository = new DapperRepository<wf_formfieldinfo>();
        }

        public List<wf_forminfo> GetData()
        {
            return wf_forminfoRepository.GetAllEnumerable().ToList();
        }

        public wf_forminfo GetModelById(string id)
        {
            return wf_forminfoRepository.GetById(id);
        }

		
        internal void Add(wf_forminfo model)
        {
            wf_forminfoRepository.Insert(model);
        }

        internal void Update(wf_forminfo model)
        {
            wf_forminfoRepository.Update(model);
        }

		internal void Del(string id)
        {
            wf_forminfoRepository.DeleteByIds(id);
        }

        public wf_formfieldinfo GetModelFieldById(string id)
        {
            return wf_formfieldinfoRepository.GetById(id);
        }

        internal List<SelectListItem> GetTYPEID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = wf_forminfoRepository.SqlWithParamsDynamic("select a.CODE_ID Value,concat(a.CODE_NAME,'-----',a.CODE_NO) Text from sys_code  a  where PARENT_NO=1");
            tables.Clear();
            //tables.Add(new SelectListItem { Value = "0", Text = "表单主分类" });
            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        public wf_formfieldinfo GetModelFieldByName(string name,string formid)
        {
            var sql = "select * from wf_formfieldinfo where NAME=@NAME and FORMID=@FORMID";
            return wf_formfieldinfoRepository.SqlWithParams(sql, new { NAME = name, FORMID = formid }).FirstOrDefault();
        }

        public int GetIDByName(string name, string formid)
        {
            var sql = "select ID from wf_formfieldinfo where NAME=@NAME and FORMID=@FORMID";
            return wf_formfieldinfoRepository.SqlWithParams(sql, new { NAME = name, FORMID = formid }).FirstOrDefault().ID;
        }

        internal void AddField(wf_formfieldinfo model)
        {
            wf_formfieldinfoRepository.Insert(model);
        }

        internal void UpdateField(wf_formfieldinfo model)
        {
            wf_formfieldinfoRepository.Update(model);
        }

        internal void DelField(string id)
        {
            wf_formfieldinfoRepository.DeleteByIds(id);
        }

    }
}